using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.Inventory;
using MSIACCSL2.Views.PreSalesOrder;
using WcfService1.DTO;

namespace MSIACCSL2.Views.DeliveryOrderElse
{
    public partial class FrmAdd : Page
    {
        private InventorySearch si;

        public DeliveryOrderElseViewModel VM
        {
            get { return (DeliveryOrderElseViewModel)this.DataContext; }
        }

        public DeliveryOrderElseWrapViewModel WMW
        {
            get { return (this.Resources["POW"] as DeliveryOrderElseWrapViewModel); }
        }

        public FrmAdd()
        {
            InitializeComponent();

            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as DeliveryOrderElseWrapViewModel).DeliveryOrderElseViewModel =
                                        (DeliveryOrderElseViewModel)this.DataContext;

                                   txtDate.Focus();
                               };

            txtSerialNumber.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    WMW.GetInventory2(txtSerialNumber.Text);
                    txtSerialNumber.Text = string.Empty;
                    
                }
            };

            this.KeyDown += (x, y) =>
            {
                if (y.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.SaveDeliveryOrderElse(VM.DeliveryOrderElse);
                else if (y.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.Cancel(null);
                else if (y.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control)
                    WMW.AddDeliveryOrderElseDetail(null);
                else if (y.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                    WMW.DeleteDeliveryOrderElseDetail(null);
            };
        }

    
        private void btnFind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            si = new InventorySearch();
            si.Closed += new EventHandler(si_Closed);
            si.Show();
        }

        void si_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            if (inv != null) txtSerialNumber.Text = inv.SerialNumber;
            txtSerialNumber.Focus();
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }

        
        private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (DeliveryOrderViewModel.mode != Mode.Edit)
        	    VM.GetQtyOnHand((sender as ComboBox).DataContext);
        }

    

        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }


        private void cboWarehouse_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            VM.WarehouseSelected = (cboWarehouse.SelectedItem as WarehouseDTO);
            foreach (var deliveryOrderDetail in VM.DeliveryOrderElse.DeliveryOrderDetails)
            {
                deliveryOrderDetail.Warehouse = VM.WarehouseSelected;
            }
        }

    
      

    }
}

